# import requests
# url = 'http://www.glidedsky.com/level/web/crawler-basic-1'
# res = requests.get(url)
# res.
import json

import requests
import re
# # url = 'https://httpbin.org/post?name=yjy'
# url = 'https://httpbin.org/get?name=yjy'
# data ={
#     'age':100,
#     'home':'龙国'
# }
# response = requests.get(url,params=data)
# if response.status_code==200:
#     print(response.content)
#     print(response.text)
#     print(response.json())


# response = requests.post(url, data)
# if response.status_code==200:
#     print(response.json())
# response = requests.post(url, json=data)
# response = requests.post(url, data)
# if response.status_code==200:
#     print(json.loads(response.json()['data']))
    # response_json = json.loads(response.text)
    # print(response_json)
    # print(response.text)

# 请求参数案例
# url = 'http://127.0.0.1:8000/admin/'
# response = requests.get(url)
# if response.status_code==200:
#     print(response.url)

# url = 'http://127.0.0.1:8000/admin/'
# #是否允许重定向
# response = requests.get(url,allow_redirects=False)
# print(response.url)
# print(response.status_code)
# if response.status_code==200:
#     print(response.url)


# 请求标头
# url = 'https://httpbin.org/get?name=yjy'
# response = requests.get(url,headers={
# 'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36'
# })
# print(response.json()['headers'].get('User-Agent'))


# cookie
# url = 'http://127.0.0.1:8000/admin/login/?next=/admin/'
# session = requests.Session()
# res = session.get(url)
# print(res.cookies)

# 模拟登陆
1.
# url = 'http://127.0.0.1:8000/admin/'
# cookies={
#     'sessionid':'f27hf62zvqm2h0clr1uks7psnozq7sla'
# }
# response = requests.get(url,cookies=cookies)
# if response.status_code==200:
#     print(response.text)


2.
# url = 'http://127.0.0.1:8000/admin/login/?next=/admin/'
# response = requests.get(url)
# # print(response.cookies)
# csrfmiddlewaretoken=re.search(r'<input type="hidden" name="csrfmiddlewaretoken" value="(.*?)">',response.text).group(1)
#     # print(csrfmiddlewaretoken)
#
#
# response_lost = requests.post(url,data={
#     'csrfmiddlewaretoken':csrfmiddlewaretoken,
#     'username':'admin',
#     'password':'123456',
#
# },cookies=response.cookies)
# print(response_lost.text)



3.
# url = 'http://127.0.0.1:8000/admin/login/?next=/admin/'
# session = requests.Session()
# res = session.get(url)
# csrfmiddlewaretoken=re.search(r'<input type="hidden" name="csrfmiddlewaretoken" value="(.*?)">',res.text).group(1)
# response_lost = session.post(url,data={
#     'csrfmiddlewaretoken':csrfmiddlewaretoken,
#     'username':'admin',
#     'password':'123456',
#
# },headers={'user-agent':
# 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36',
# 'Referer': url,
# })
# print(response_lost.request.headers['Referer'])
# print(response_lost.text)



#Referer
# url = 'http://127.0.0.1:8000/admin/login/?next=/admin/'
# response = requests.post(url)
# # if response.status_code==200:
# print(response.url)
# print(response.request.headers.get('referer'))